package com.baichang.taoshunda.service.impl;

import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.dao.PensionDao;
import com.baichang.taoshunda.entity.Pension;
import com.baichang.taoshunda.entity.User;
import com.baichang.taoshunda.service.PensionService;
import com.baichang.taoshunda.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @param
 * @author: wsl
 * @Date: 2018/11/30 09：19
 * @Description:
 */
@Service
public class PensionServiceImpl implements PensionService {

    @Autowired
    private PensionDao pensionDao;

    @Autowired
    private UserService userService;

    /**
     * 功能描述:
     * 添加养老保险金
     *
     * @param userId      用户编号
     * @param name        来源名称 （订单显示第一个商品的名称）
     * @param type        来源类型
     * @param value       商品的规格值、话费充值的手机号
     * @param orderNumber 订单编号
     * @param money       补贴金额
     * @param monetary    消费金额
     * @param pension     养老金
     * @param operator    1 加  2 减
     * @param goodsId
     * @return:
     * @auther: wsl
     * @date: 2018/11/30 9:27
     */
    @Override
    public boolean savePension(Integer userId, String name, Integer type, String value, String orderNumber, BigDecimal money, BigDecimal monetary, BigDecimal pension, Integer operator, Integer companyId, Integer payType, String address, BigDecimal redPacket, Integer goodsId) {
        pension = getPension(userId, pension, operator);
        Pension pensionObject = new Pension();
        pensionObject.setOrderNumber(orderNumber);
        pensionObject.setUserId(userId);
        pensionObject.setName(name);
        pensionObject.setValue(value);
        pensionObject.setCreated(new Date());
        pensionObject.setMonetary(monetary);
        pensionObject.setType(type);
        pensionObject.setMoney(money);
        pensionObject.setPension(pension);
        pensionObject.setCompanyId(companyId);
        pensionObject.setAddress(address);
        pensionObject.setGoodsId(goodsId);
        pensionObject.setPayType(payType);
        pensionObject.setRedPacket(redPacket);
        return pensionDao.save(pensionObject) > 0;
    }

    /**
     * 功能描述:
     *
     * @param: 查询我的养老金
     * @return:
     * @auther: wsl
     * @date: 2018/11/30 14:51
     */
    @Override
    public List<Map<String, Object>> findPensionList(Integer userId, Integer nowPage, Integer pageSize) {
        List<Map<String, Object>> pensionList = pensionDao.findPension(userId, nowPage, pageSize);

        if (pensionList.isEmpty()) {
            return new ArrayList<>();
        }
        return pensionDao.findPension(userId, nowPage, pageSize);
    }

    @Override
    public Map<String, Object> getUserPensionByUserId(Integer userId) throws ServiceException {
        Map map = new HashMap();
        User user = userService.getUserBeanById(userId);
        Pension pension = pensionDao.getPensionByUserId(userId);
        //计算两个日期相差年数
        //获取日历实例
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(user.getCreated());
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(new Date());
        int year = calEnd.get(Calendar.YEAR) - calBegin.get(Calendar.YEAR);
        int month = calEnd.get(Calendar.MONTH) - calBegin.get(Calendar.MONTH);
        map.put("headPic", user.getHeadPic());
        map.put("userName", user.getNickName());
        map.put("cardName", user.getCardName());
        map.put("sex", user.getSex());
        if (pension != null) {
            map.put("pension", pension.getPension());
        } else {
            map.put("pension", 0);
        }
        map.put("year", year);
        map.put("month", month % 12);
        return map;
    }

    /**
     * 功能描述:
     * 计算获取保险金额
     *
     * @param userId   用户编号
     * @param pension  养老金
     * @param operator 1 加  2 减
     * @return:
     * @auther: wsl
     * @date: 2018/11/30 9:38
     */
    private BigDecimal getPension(Integer userId, BigDecimal pension, Integer operator) {
        Pension pension1 = pensionDao.getPensionByUserId(userId);
        if (pension1 == null) {
            pension = BigDecimal.valueOf(0).add(pension);
        } else {
            if (operator == 1) {
                pension = pension1.getPension().add(pension);
            } else {
                pension = pension1.getPension().subtract(pension);

            }
        }
        return pension;
    }
}
